home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrscript / feature files / vrhash.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  11.5 KB  |  283 lines

  1. //////////
  2. //
  3. //    File:        VRHash.c
  4. //
  5. //    Contains:    Functions for hash table management.
  6. //
  7. //    Written by:    Tim Monroe
  8. //
  9. //    Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <4>         12/02/98        rtm        renamed VRHash_CreateHashTable and _DestroyHashTable to _Init and _Stop
  14. //       <3>         12/02/98        rtm        simplified VRHash_DestroyHashTable
  15. //       <2>         11/29/98        rtm        minor clean-up to some routines
  16. //       <1>         11/28/98        rtm        first file (Happy Thanksgiving!)
  17. //    
  18. //    Our hash table is an array of "bucket pointers"; each bucket (defined by the VRScriptHash
  19. //    data structure) contains the command word, its corresponding command code, and a pointer to
  20. //    the next bucket in the linked list (or NULL if there is no next bucket).
  21. //
  22. //    There are many ways to implement a hash table management scheme; the one used here is eerily
  23. //    similar (as I now discover) to the one described by Kernighan & Ritchie in The C Programming
  24. //    Language.
  25. //
  26. //////////
  27.  
  28. #include "VRHash.h"
  29.  
  30. // global variables
  31.  
  32. static VRScriptHashPtr            gHashTable[kNumEntriesInTable];            // the hash table
  33.  
  34.  
  35. //////////
  36. //
  37. // VRHash_Init
  38. // Create the hash table.
  39. //
  40. //////////
  41.  
  42. void VRHash_Init (void)
  43. {
  44.     short            myIndex;
  45.         
  46.     // initialize the bucket pointers in the hash table
  47.     for (myIndex = 0; myIndex < kNumEntriesInTable; myIndex++)
  48.         gHashTable[myIndex] = NULL;
  49.     
  50.     // insert entries into the hash table
  51.     VRHash_PutCommandIntoTable("SetVerboseState", kSetVerboseState);
  52.     VRHash_PutCommandIntoTable("OpenQTVRMovieFile", kOpenQTVRMovieFile);
  53.     VRHash_PutCommandIntoTable("ReplaceMainMovie", kReplaceMainMovie);
  54.     VRHash_PutCommandIntoTable("SetCurrentDirectory", kSetCurrentDirectory);
  55.     VRHash_PutCommandIntoTable("SetBarState", kSetBarState);
  56.     VRHash_PutCommandIntoTable("SetButtonState", kSetButtonState);
  57.     VRHash_PutCommandIntoTable("SetResizeState", kSetResizeState);
  58.     VRHash_PutCommandIntoTable("SetWindowSize", kSetWindowSize);
  59.     VRHash_PutCommandIntoTable("SetMaxWindowSize", kSetMaxWindowSize);
  60.     VRHash_PutCommandIntoTable("ReplaceCursor", kReplaceCursor);
  61.     VRHash_PutCommandIntoTable("SetHotSpotIDCursors", kSetHotSpotIDCursors);
  62.     VRHash_PutCommandIntoTable("SetHotSpotTypeCursors", kSetHotSpotTypeCursors);
  63.     VRHash_PutCommandIntoTable("GoToNodeID", kGoToNodeID);
  64.     VRHash_PutCommandIntoTable("ShowDefaultView", kShowDefaultView);
  65.     VRHash_PutCommandIntoTable("OpenResourceFile", kOpenResourceFile);
  66.     VRHash_PutCommandIntoTable("SetCorrection", kSetCorrection);
  67.     VRHash_PutCommandIntoTable("SetQuality", kSetQuality);
  68.     VRHash_PutCommandIntoTable("SetSwingSpeed", kSetSwingSpeed);
  69.     VRHash_PutCommandIntoTable("SetSwingDirection", kSetSwingDirection);
  70.     VRHash_PutCommandIntoTable("SetSwingState", kSetSwingState);
  71.     VRHash_PutCommandIntoTable("SetPanAngle", kSetPanAngle);
  72.     VRHash_PutCommandIntoTable("SetTiltAngle", kSetTiltAngle);
  73.     VRHash_PutCommandIntoTable("SetPanTiltZoom", kSetPanTiltZoom);
  74.     VRHash_PutCommandIntoTable("SetFieldOfView", kSetFieldOfView);
  75.     VRHash_PutCommandIntoTable("SetViewCenter", kSetViewCenter);
  76.     VRHash_PutCommandIntoTable("SetPanLimits", kSetPanLimits);
  77.     VRHash_PutCommandIntoTable("SetTiltLimits", kSetTiltLimits);
  78.     VRHash_PutCommandIntoTable("SetZoomLimits", kSetZoomLimits);
  79.     VRHash_PutCommandIntoTable("SetHotSpotState", kSetHotSpotState);
  80.     VRHash_PutCommandIntoTable("SetTranslateState", kSetTranslateState);
  81.     VRHash_PutCommandIntoTable("SetClickRadius", kSetClickRadius);
  82.     VRHash_PutCommandIntoTable("SetClickTimeout", kSetClickTimeout);
  83.     VRHash_PutCommandIntoTable("SetPanTiltSpeed", kSetPanTiltSpeed);
  84.     VRHash_PutCommandIntoTable("SetZoomSpeed", kSetZoomSpeed);
  85.     VRHash_PutCommandIntoTable("SetMouseScale", kSetMouseScale);
  86.     VRHash_PutCommandIntoTable("SetFrameRate", kSetFrameRate);
  87.     VRHash_PutCommandIntoTable("SetViewRate", kSetViewRate);
  88.     VRHash_PutCommandIntoTable("SetViewTime", kSetViewTime);
  89.     VRHash_PutCommandIntoTable("SetViewState", kSetViewState);
  90.     VRHash_PutCommandIntoTable("SetAnimationState", kSetAnimationState);
  91.     VRHash_PutCommandIntoTable("SetControlState", kSetControlState);
  92.     VRHash_PutCommandIntoTable("SetFrameAnimState", kSetFrameAnimState);
  93.     VRHash_PutCommandIntoTable("SetViewAnimState", kSetViewAnimState);
  94.     VRHash_PutCommandIntoTable("SetQTVRVisState", kSetQTVRVisState);
  95.     VRHash_PutCommandIntoTable("SetCachePrefs", kSetCachePrefs);
  96.     VRHash_PutCommandIntoTable("SetMovieVolume", kSetMovieVolume);
  97.     VRHash_PutCommandIntoTable("SetTrackVolume", kSetTrackVolume);
  98.     VRHash_PutCommandIntoTable("SetSoundVolume", kSetSoundVolume);
  99.     VRHash_PutCommandIntoTable("SetSoundBalance", kSetSoundBalance);
  100.     VRHash_PutCommandIntoTable("PlaySceneSound", kPlaySceneSound);
  101.     VRHash_PutCommandIntoTable("PlaySceneQTMidi", kPlaySceneQTMidi);
  102.     VRHash_PutCommandIntoTable("PlayNodeQTMidi", kPlayNodeQTMidi);
  103.     VRHash_PutCommandIntoTable("PlayNodeSound", kPlayNodeSound);
  104.     VRHash_PutCommandIntoTable("PlayNode3DSound", kPlayNode3DSound);
  105.     VRHash_PutCommandIntoTable("HotSpotQTMidi", kHotSpotQTMidi);
  106.     VRHash_PutCommandIntoTable("HotSpotSound", kHotSpotSound);
  107.     VRHash_PutCommandIntoTable("HotSpot3DSound", kHotSpot3DSound);
  108.     VRHash_PutCommandIntoTable("HotSpotMovie", kHotSpotMovie);
  109.     VRHash_PutCommandIntoTable("TriggerHotSpot", kTriggerHotSpot);
  110.     VRHash_PutCommandIntoTable("PlayQTMidi", kPlayQTMidi);
  111.     VRHash_PutCommandIntoTable("PlaySndResource", kPlaySndResource);
  112.     VRHash_PutCommandIntoTable("PlaySound", kPlaySndResource);                // synonym
  113.     VRHash_PutCommandIntoTable("PlaySoundFile", kPlaySoundFile);
  114.     VRHash_PutCommandIntoTable("Play3DSndResource", kPlay3DSndResource);
  115.     VRHash_PutCommandIntoTable("Play3DSndResourceAngle", kPlay3DSndResourceAngle);
  116.     VRHash_PutCommandIntoTable("ShowPicture", kShowPicture);
  117.     VRHash_PutCommandIntoTable("ShowNodePicture", kShowNodePicture);
  118.     VRHash_PutCommandIntoTable("AtTime", kAtTime);
  119.     VRHash_PutCommandIntoTable("AtAppLaunch", kAtAppLaunch);
  120.     VRHash_PutCommandIntoTable("AtAppQuit", kAtAppQuit);
  121.     VRHash_PutCommandIntoTable("AtMouseOverHSID", kAtMouseOverHSID);
  122.     VRHash_PutCommandIntoTable("AtMouseOverHSType", kAtMouseOverHSType);
  123.     VRHash_PutCommandIntoTable("AtClickHSID", kAtClickHSID);
  124.     VRHash_PutCommandIntoTable("AtClickHS", kAtClickHSID);                    // synonym
  125.     VRHash_PutCommandIntoTable("AtClickHSType", kAtClickHSType);
  126.     VRHash_PutCommandIntoTable("AtClickCustomButton", kAtClickCustomButton);
  127.     VRHash_PutCommandIntoTable("AtClickSprite", kAtClickSprite);
  128.     VRHash_PutCommandIntoTable("AtNodeEntry", kAtNodeEntry);
  129.     VRHash_PutCommandIntoTable("AtNodeExit", kAtNodeExit);
  130.     VRHash_PutCommandIntoTable("AtPanAngle", kAtPanAngle);
  131.     VRHash_PutCommandIntoTable("AtTiltAngle", kAtTiltAngle);
  132.     VRHash_PutCommandIntoTable("AtZoomAngle", kAtZoomAngle);
  133.     VRHash_PutCommandIntoTable("DoBoth", kDoBoth);
  134.     VRHash_PutCommandIntoTable("DoNothing", kDoNothing);
  135.     VRHash_PutCommandIntoTable("PlayMovie", kPlayMovie);
  136.     VRHash_PutCommandIntoTable("PlayTransMovie", kPlayTransMovie);
  137.     VRHash_PutCommandIntoTable("PlayTransEffect", kPlayTransEffect);
  138.     VRHash_PutCommandIntoTable("MoveScreen", kMoveScreen);
  139.     VRHash_PutCommandIntoTable("Beep", kBeep);
  140.     VRHash_PutCommandIntoTable("ProcessScript", kProcessScript);
  141.     VRHash_PutCommandIntoTable("CreateBox", kCreateBox);
  142.     VRHash_PutCommandIntoTable("CreateCone", kCreateCone);
  143.     VRHash_PutCommandIntoTable("CreateCylinder", kCreateCylinder);
  144.     VRHash_PutCommandIntoTable("CreateEllipsoid", kCreateEllipsoid);
  145.     VRHash_PutCommandIntoTable("CreateTorus", kCreateTorus);
  146.     VRHash_PutCommandIntoTable("CreateRectangle", kCreateRectangle);
  147.     VRHash_PutCommandIntoTable("Open3DMFFile", kOpen3DMFFile);
  148.     VRHash_PutCommandIntoTable("Set3DObjLocation", kSet3DObjLocation);
  149.     VRHash_PutCommandIntoTable("Set3DObjColor", kSet3DObjColor);
  150.     VRHash_PutCommandIntoTable("Set3DObjTransp", kSet3DObjTransp);
  151.     VRHash_PutCommandIntoTable("Set3DObjInterp", kSet3DObjInterp);
  152.     VRHash_PutCommandIntoTable("Set3DObjBackface", kSet3DObjBackface);
  153.     VRHash_PutCommandIntoTable("Set3DObjFill", kSet3DObjFill);
  154.     VRHash_PutCommandIntoTable("Set3DObjRotation", kSet3DObjRotation);
  155.     VRHash_PutCommandIntoTable("Set3DObjRotState", kSet3DObjRotState);
  156.     VRHash_PutCommandIntoTable("Set3DObjVisState", kSet3DObjVisState);
  157.     VRHash_PutCommandIntoTable("Set3DObjTexture", kSet3DObjTexture);
  158.     VRHash_PutCommandIntoTable("Destroy3DObject", kDestroy3DObject);
  159.     VRHash_PutCommandIntoTable("Set3DSndLocation", kSet3DSndLocation);
  160.     VRHash_PutCommandIntoTable("SetVariable", kSetVariable);
  161.     VRHash_PutCommandIntoTable("If", kIf);
  162.     VRHash_PutCommandIntoTable("SetSpriteVisState", kSetSpriteVisState);
  163.     VRHash_PutCommandIntoTable("SetSpriteLayer", kSetSpriteLayer);
  164.     VRHash_PutCommandIntoTable("SetSpriteGraphicsMode", kSetSpriteGraphicsMode);
  165.     VRHash_PutCommandIntoTable("SetSpriteImageIndex", kSetSpriteImageIndex);
  166.     VRHash_PutCommandIntoTable("SetSpriteMatrix", kSetSpriteMatrix);
  167.     VRHash_PutCommandIntoTable("SetSpriteLocation", kSetSpriteLocation);
  168.     VRHash_PutCommandIntoTable("SetTrackState", kSetTrackState);
  169.     VRHash_PutCommandIntoTable("SetTrackLayer", kSetTrackLayer);
  170.     VRHash_PutCommandIntoTable("SetMovieTime", kSetMovieTime);
  171.     VRHash_PutCommandIntoTable("SetMovieRate", kSetMovieRate);
  172.     VRHash_PutCommandIntoTable("SetMovieTimeScale", kSetMovieTimeScale);
  173. }
  174.  
  175.  
  176. //////////
  177. //
  178. // VRHash_Stop
  179. // Destroy the hash table.
  180. //
  181. //////////
  182.  
  183. void VRHash_Stop (void)
  184. {
  185.     UInt32                myIndex;
  186.     VRScriptHashPtr        myBucketPtr;
  187.     VRScriptHashPtr        myNextBucketPtr;
  188.  
  189.     for (myIndex = 0; myIndex < kNumEntriesInTable; myIndex++) {
  190.     
  191.         // get the bucket pointer at this offset in the hash table
  192.         myBucketPtr = gHashTable[myIndex];
  193.  
  194.         while (myBucketPtr != NULL) {
  195.             myNextBucketPtr = myBucketPtr->fNextEntry;
  196.             free(myBucketPtr->fCommandWord);
  197.             DisposePtr((Ptr)myBucketPtr);
  198.             myBucketPtr = myNextBucketPtr;
  199.         }
  200.     }
  201. }
  202.  
  203.  
  204. //////////
  205. //
  206. // VRHash_HashCommandWord
  207. // Get the hash value for the specified command word.
  208. //
  209. // This hash function returns a value in the range 0 to kNumEntriesInTable - 1. It's
  210. // a simple additive hash function. (As K&R put it, "[t]his is not the best possible
  211. // algorithm, but it has the merit of extreme simplicity".)
  212. //
  213. //////////
  214.  
  215. UInt32 VRHash_HashCommandWord (char *theCommandWord)
  216. {
  217.     UInt32        myHashVal;
  218.  
  219.     for (myHashVal = 0; *theCommandWord != '\0'; myHashVal += *theCommandWord++)
  220.         ;
  221.  
  222.     return(myHashVal % kNumEntriesInTable);
  223. }
  224.  
  225.  
  226. //////////
  227. //
  228. // VRHash_PutCommandIntoTable
  229. // Insert an entry for the specified command word and command code into the hash table.
  230. //
  231. //////////
  232.  
  233. void VRHash_PutCommandIntoTable (char *theCommandWord, UInt32 theCommandCode)
  234. {
  235.     UInt32                myHashVal;
  236.     VRScriptHashPtr        myBucketPtr;
  237.     
  238.     if (theCommandWord == NULL)
  239.         return;
  240.  
  241.     // get the hash value for the specified command word
  242.     myHashVal = VRHash_HashCommandWord(theCommandWord);
  243.     
  244.     // create a new bucket and insert it at the head of the linked list
  245.     myBucketPtr = (VRScriptHashPtr)NewPtrClear(sizeof(VRScriptHash));
  246.     if (myBucketPtr != NULL) {
  247.         myBucketPtr->fCommandCode = theCommandCode;
  248.         myBucketPtr->fCommandWord = malloc(strlen(theCommandWord) + 1);
  249.         strncpy(myBucketPtr->fCommandWord, theCommandWord, strlen(theCommandWord) + 1);
  250.         myBucketPtr->fNextEntry = gHashTable[myHashVal];
  251.         gHashTable[myHashVal] = myBucketPtr;
  252.     }
  253. }
  254.  
  255.  
  256. //////////
  257. //
  258. // VRHash_GetCommandCode
  259. // Get the command code associated with the specified command word.
  260. //
  261. //////////
  262.  
  263. UInt32 VRHash_GetCommandCode (char *theCommandWord)
  264. {
  265.     UInt32                myCode = kInvalidCommand;
  266.     UInt32                myHashVal;
  267.     VRScriptHashPtr        myBucketPtr;
  268.     
  269.     if (theCommandWord == NULL)
  270.         return(myCode);
  271.  
  272.     // get the hash value for the specified command word
  273.     myHashVal = VRHash_HashCommandWord(theCommandWord);
  274.     
  275.     for (myBucketPtr = gHashTable[myHashVal]; myBucketPtr != NULL; myBucketPtr = myBucketPtr->fNextEntry)
  276.         if (strcmp(theCommandWord, myBucketPtr->fCommandWord) == 0)
  277.             return(myBucketPtr->fCommandCode);
  278.  
  279.     return(myCode);
  280. }
  281.  
  282.  
  283.